package day01;

/**
 * @author feng
 * @Date 2021/7/8 16:17
 */

// 模拟龟兔赛跑问题
public class ThreadTest05 implements Runnable {
      private static String winnner;

    @Override
    public void run() {
        for (int i = 0; i <=100 ; i++) {

            //模拟兔子睡觉
            if (Thread.currentThread().getName().equals("兔子") && i%10==0){
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            //判断
           boolean flag = gameover(i);
              if (flag){
                  break;
              }

            System.out.println(Thread.currentThread().getName()+"跑了" + i + "步");
        }

    }

    // 判断比赛是否结束
    private  boolean gameover(int i){

        if (winnner!=null){
            return true;
        }{
            if (i>=100) {
                winnner = Thread.currentThread().getName();
                System.out.println("winner is" + winnner);

            }
        }
        return false;
    }

    public static void main(String[] args) {

       ThreadTest05 test05 = new ThreadTest05();

       new Thread(test05,"兔子").start();
       new Thread(test05,"乌龟").start();

    }

}
